package org.springblade.modules.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.platform.dto.*;
import org.springblade.modules.platform.entity.DeptBenchInfo;
import org.springblade.modules.platform.entity.DeptInfoTable;
import org.springblade.modules.platform.entity.DeptMeasuringInfo;
import org.springblade.modules.platform.entity.DeptTypeInfo;
import org.springblade.modules.platform.mapper.DeptBenchInfoMapper;
import org.springblade.modules.platform.mapper.DeptInfoTableMapper;
import org.springblade.modules.platform.mapper.DeptMeasuringInfoMapper;
import org.springblade.modules.platform.mapper.DeptTypeInfoMapper;
import org.springblade.modules.platform.service.IDeptInfoTableService;
import org.springblade.modules.platform.vo.*;
import org.springblade.modules.platform.vo.enums.BenchTypeNameEnum;
import org.springblade.modules.platform.vo.enums.DeptCategoryEnum;
import org.springblade.modules.platform.vo.enums.MeasureTypeEnum;
import org.springblade.modules.platform.vo.enums.UntilTypeEnum;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYG
 * @since 2022-08-26
 */
@Service
@AllArgsConstructor
public class DeptInfoTableServiceImpl extends ServiceImpl<DeptInfoTableMapper, DeptInfoTable> implements IDeptInfoTableService {

    @Autowired
    DeptTypeInfoMapper deptTypeInfoMapper;
    @Autowired
    DeptBenchInfoMapper deptBenchInfoMapper;
    @Autowired
    DeptMeasuringInfoMapper deptMeasuringInfoMapper;


    @Override
    public DeptInfoTable getInfo(Long deptId) {
        return baseMapper.selectOne(Wrappers.<DeptInfoTable>query().lambda().eq(DeptInfoTable::getDeptId,deptId).eq(DeptInfoTable::getIsDeleted,0));
    }

    @Override
    public void saveDeptInfoTable(DeptInfoTable deptInfoTable) {
        deptInfoTable.setInsertTime(System.currentTimeMillis());
        deptInfoTable.setUpdateTime(System.currentTimeMillis());
        deptInfoTable.setIsDeleted(0);
        baseMapper.insert(deptInfoTable);
    }

    @Override
    public void updateDeptInfo(DeptInfoTableDTO deptInfoTableDTO) {
        DeptInfoTable deptInfoTable = new DeptInfoTable();
        BeanUtils.copyProperties(deptInfoTableDTO,deptInfoTable);
        deptInfoTable.setUpdateTime(System.currentTimeMillis());
        deptInfoTable.setIsDeleted(0);
        baseMapper.updateById(deptInfoTable);


        DeptTypeInfoDTO deptTypeInfoDTO = deptInfoTableDTO.getDeptTypeInfoDTO();
        if(deptTypeInfoDTO != null){
            DeptTypeInfo deptTypeInfo = deptTypeInfoMapper.selectOne(Wrappers.<DeptTypeInfo>query().lambda()
                    .eq(DeptTypeInfo::getDeptId, deptInfoTableDTO.getId()));
            if(deptTypeInfo == null){
                deptTypeInfo = new DeptTypeInfo();
                BeanUtils.copyProperties(deptTypeInfoDTO,deptTypeInfo);
                deptTypeInfo.setDeptId(deptInfoTableDTO.getId());
                deptTypeInfo.setUnitTypeName(deptTypeInfoDTO.getUntilTypeEnum().getMsg());
                deptTypeInfo.setUnitTypeCode(deptTypeInfoDTO.getUntilTypeEnum().getCode());
                deptTypeInfo.setPosition(deptTypeInfoDTO.getPosition());
                deptTypeInfoMapper.insert(deptTypeInfo);
            }else {
                BeanUtils.copyProperties(deptTypeInfoDTO,deptTypeInfo);
                deptTypeInfo.setDeptId(deptInfoTableDTO.getId());
                deptTypeInfo.setUnitTypeName(deptTypeInfoDTO.getUntilTypeEnum()==null?null:deptTypeInfoDTO.getUntilTypeEnum().getMsg());
                deptTypeInfo.setUnitTypeCode(deptTypeInfoDTO.getUntilTypeEnum()==null?null:deptTypeInfoDTO.getUntilTypeEnum().getCode());
                deptTypeInfo.setPosition(deptTypeInfoDTO.getPosition());
                deptTypeInfoMapper.updateById(deptTypeInfo);

            }
        }
        DeptBenchInfoDTO deptBenchInfoDTO = deptInfoTableDTO.getDeptBenchInfoDTO();
        if(deptBenchInfoDTO != null){
            DeptBenchInfo deptBenchInfo = deptBenchInfoMapper.selectOne(Wrappers.<DeptBenchInfo>query().lambda()
                    .eq(DeptBenchInfo::getDeptId, deptInfoTableDTO.getId()));
            if(deptBenchInfo == null){
                deptBenchInfo = new DeptBenchInfo();
                BeanUtils.copyProperties(deptBenchInfoDTO,deptBenchInfo);
                deptBenchInfo.setDeptId(deptInfoTableDTO.getId());
                deptBenchInfo.setBenchTypeName(deptBenchInfoDTO.getBenchTypeNameEnum()==null?null:deptBenchInfoDTO.getBenchTypeNameEnum().getMsg());
                deptBenchInfo.setBenchTypeCode(deptBenchInfoDTO.getBenchTypeNameEnum()==null?null:deptBenchInfoDTO.getBenchTypeNameEnum().getCode());
                deptBenchInfo.setPosition(deptBenchInfoDTO.getPosition());
                deptBenchInfoMapper.insert(deptBenchInfo);
            }else {
                BeanUtils.copyProperties(deptBenchInfoDTO,deptBenchInfo);
                deptBenchInfo.setDeptId(deptInfoTableDTO.getId());
                deptBenchInfo.setBenchTypeName(deptBenchInfoDTO.getBenchTypeNameEnum()==null?null:deptBenchInfoDTO.getBenchTypeNameEnum().getMsg());
                deptBenchInfo.setBenchTypeCode(deptBenchInfoDTO.getBenchTypeNameEnum()==null?null:deptBenchInfoDTO.getBenchTypeNameEnum().getCode());
                deptBenchInfo.setPosition(deptBenchInfoDTO.getPosition());
                deptBenchInfo.setParentBench(deptBenchInfoDTO.getParentBench());
                deptBenchInfo.setParentBenchName(deptBenchInfoDTO.getParentBenchName());
                deptBenchInfoMapper.updateById(deptBenchInfo);
            }
        }
        DeptMeasuringInfoDTO deptMeasuringInfoDTO = deptInfoTableDTO.getDeptMeasuringInfoDTO();
        if(deptMeasuringInfoDTO != null){
            DeptMeasuringInfo deptMeasuringInfo = deptMeasuringInfoMapper.selectOne(Wrappers.<DeptMeasuringInfo>query().lambda()
                    .eq(DeptMeasuringInfo::getDeptId, deptInfoTableDTO.getId()));
            if(deptMeasuringInfo == null){
                deptMeasuringInfo = new DeptMeasuringInfo();
                BeanUtils.copyProperties(deptMeasuringInfoDTO,deptMeasuringInfo);
                deptMeasuringInfo.setDeptId(deptInfoTableDTO.getId());
                deptMeasuringInfo.setMeasuringTypeName(deptMeasuringInfoDTO.getMeasureTypeEnum()==null?null:deptMeasuringInfoDTO.getMeasureTypeEnum().getMsg());
                deptMeasuringInfo.setMeasuringTypeCode(deptMeasuringInfoDTO.getMeasureTypeEnum()==null?null:deptMeasuringInfoDTO.getMeasureTypeEnum().getCode());
                deptMeasuringInfoMapper.insert(deptMeasuringInfo);
            }else {
                BeanUtils.copyProperties(deptMeasuringInfoDTO,deptMeasuringInfo);
                deptMeasuringInfo.setDeptId(deptInfoTableDTO.getId());
                deptMeasuringInfo.setMeasuringTypeName(deptMeasuringInfoDTO.getMeasureTypeEnum()==null?null:deptMeasuringInfoDTO.getMeasureTypeEnum().getMsg());
                deptMeasuringInfo.setMeasuringTypeCode(deptMeasuringInfoDTO.getMeasureTypeEnum()==null?null:deptMeasuringInfoDTO.getMeasureTypeEnum().getCode());

                deptMeasuringInfoMapper.updateById(deptMeasuringInfo);
            }
        }
    }

    @Override
    public DeptInfoTableInfoVO getDeptInfoTableInfo(Integer id) {
        DeptInfoTable deptInfoTable = baseMapper.selectById(id);
        DeptInfoTableInfoVO deptInfoTableInfoVO = new DeptInfoTableInfoVO();
        BeanUtils.copyProperties(deptInfoTable,deptInfoTableInfoVO);

        DeptTypeInfo deptTypeInfo = deptTypeInfoMapper.selectOne(Wrappers.<DeptTypeInfo>query().lambda()
                .eq(DeptTypeInfo::getDeptId, id));
        DeptTypeInfoVO deptTypeInfoVO = new DeptTypeInfoVO();
        if(deptTypeInfo != null){
            BeanUtils.copyProperties(deptTypeInfo,deptTypeInfoVO);
            deptTypeInfoVO.setUntilTypeEnum(deptTypeInfo.getUnitTypeCode()==null?null:UntilTypeEnum.getEnum(deptTypeInfo.getUnitTypeCode()));
        }else {
            deptTypeInfoVO.setUntilTypeEnum(null);
        }
        deptInfoTableInfoVO.setDeptTypeInfoVO(deptTypeInfoVO);
        DeptBenchInfo deptBenchInfo = deptBenchInfoMapper.selectOne(Wrappers.<DeptBenchInfo>query().lambda()
                .eq(DeptBenchInfo::getDeptId, id));
        DeptBenchInfoVO deptBenchInfoVO = new DeptBenchInfoVO();
        if(deptBenchInfo != null){
            BeanUtils.copyProperties(deptBenchInfo,deptBenchInfoVO);
            deptBenchInfoVO.setBenchTypeNameEnum(deptBenchInfo.getBenchTypeCode()==null?null:BenchTypeNameEnum.getEnum(deptBenchInfo.getBenchTypeCode()).name());
            deptInfoTableInfoVO.setDeptBenchInfoVO(deptBenchInfoVO);

        }else {
        /*    DeptInfoTable deptInfoTable1 = previousMeasureType(id);
            if(deptInfoTable1 !=null){
                deptBenchInfoVO.setPreviousTypeCode(deptInfoTable1.getId());
                deptBenchInfoVO.setPreviousTypeName(deptInfoTable1.getDeptName());
            }*/
            deptBenchInfoVO.setBenchTypeNameEnum(null);

            deptInfoTableInfoVO.setDeptBenchInfoVO(deptBenchInfoVO);
        }
        DeptMeasuringInfo deptMeasuringInfo = deptMeasuringInfoMapper.selectOne(Wrappers.<DeptMeasuringInfo>query().lambda()
                .eq(DeptMeasuringInfo::getDeptId, id));
        DeptMeasuringInfoVO deptMeasuringInfoVO = new DeptMeasuringInfoVO();
        if(deptMeasuringInfo != null){
            BeanUtils.copyProperties(deptMeasuringInfo,deptMeasuringInfoVO);
            deptMeasuringInfoVO.setMeasureTypeEnum(deptMeasuringInfo.getMeasuringTypeCode()==null?null:MeasureTypeEnum.getEnum(deptMeasuringInfo.getMeasuringTypeCode()).name());
            deptInfoTableInfoVO.setDeptMeasuringInfoVO(deptMeasuringInfoVO);
        }else {
            deptMeasuringInfoVO.setMeasureTypeEnum(null);
        }
        deptInfoTableInfoVO.setDeptMeasuringInfoVO(deptMeasuringInfoVO);
        return deptInfoTableInfoVO;
    }

    @Override
    public DeptInfoTable getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void insertDeptInfoTable(DeptInfoTableDTO deptInfoTableDTO) {
        Integer parentId = deptInfoTableDTO.getParentId();
        DeptInfoTable deptInfoTable1 = baseMapper.selectById(parentId);
        DeptInfoTable deptInfoTable = new DeptInfoTable();
        BeanUtils.copyProperties(deptInfoTableDTO,deptInfoTable);
        if(deptInfoTable1==null){
            deptInfoTable.setAncestors("0");
        }else {
            deptInfoTable.setAncestors(deptInfoTable1.getAncestors()+","+deptInfoTable1.getId());
        }
        deptInfoTable.setIsDeleted(0);
        deptInfoTable.setInsertTime(System.currentTimeMillis());
        deptInfoTable.setUpdateTime(System.currentTimeMillis());
        baseMapper.insert(deptInfoTable);

        DeptTypeInfoDTO deptTypeInfoDTO = deptInfoTableDTO.getDeptTypeInfoDTO();
        if(deptTypeInfoDTO != null){
            DeptTypeInfo deptTypeInfo = new DeptTypeInfo();
            deptTypeInfo.setDeptId(deptInfoTable.getId());
            deptTypeInfo.setUnitTypeName(deptTypeInfoDTO.getUntilTypeEnum()==null?null:deptTypeInfoDTO.getUntilTypeEnum().getMsg());
            deptTypeInfo.setUnitTypeCode(deptTypeInfoDTO.getUntilTypeEnum()==null?null:deptTypeInfoDTO.getUntilTypeEnum().getCode());
            deptTypeInfo.setPosition(deptTypeInfoDTO.getPosition());
            deptTypeInfoMapper.insert(deptTypeInfo);
        }
        DeptBenchInfoDTO deptBenchInfoDTO = deptInfoTableDTO.getDeptBenchInfoDTO();
        if(deptBenchInfoDTO != null){
            DeptBenchInfo deptBenchInfo = new DeptBenchInfo();
            BeanUtils.copyProperties(deptBenchInfoDTO,deptBenchInfo);
            deptBenchInfo.setDeptId(deptInfoTable.getId());
            deptBenchInfo.setBenchTypeName(deptBenchInfoDTO.getBenchTypeNameEnum()==null?null:deptBenchInfoDTO.getBenchTypeNameEnum().getMsg());
            deptBenchInfo.setBenchTypeCode(deptBenchInfoDTO.getBenchTypeNameEnum()==null?null:deptBenchInfoDTO.getBenchTypeNameEnum().getCode());
            deptBenchInfo.setPosition(deptBenchInfoDTO.getPosition());
            deptBenchInfoMapper.insert(deptBenchInfo);
        }
        DeptMeasuringInfoDTO deptMeasuringInfoDTO = deptInfoTableDTO.getDeptMeasuringInfoDTO();
        if(deptMeasuringInfoDTO != null) {
            DeptMeasuringInfo deptMeasuringInfo = new DeptMeasuringInfo();
            BeanUtils.copyProperties(deptMeasuringInfoDTO, deptMeasuringInfo);
            deptMeasuringInfo.setDeptId(deptInfoTable.getId());
            deptMeasuringInfo.setMeasuringTypeName(deptMeasuringInfoDTO.getMeasureTypeEnum() == null ? null : deptMeasuringInfoDTO.getMeasureTypeEnum().getMsg());
            deptMeasuringInfo.setMeasuringTypeCode(deptMeasuringInfoDTO.getMeasureTypeEnum() == null ? null : deptMeasuringInfoDTO.getMeasureTypeEnum().getCode());
            deptMeasuringInfoMapper.insert(deptMeasuringInfo);
        }
    }
    @Override
    public DeptInfoTable previousMeasureType(Integer id) {
        DeptInfoTable deptInfoTable = baseMapper.selectById(id);
        Integer sort = deptInfoTable.getSort()-1;
        List<DeptInfoTable> deptInfoTables = baseMapper.selectList(Wrappers.<DeptInfoTable>query().lambda().eq(DeptInfoTable::getParentId, deptInfoTable.getParentId()));
        if(!CollectionUtils.isEmpty(deptInfoTables)){
            Optional<DeptInfoTable> first = deptInfoTables.stream().filter(x -> x.getSort() == sort).findFirst();
            if(first.isPresent()){
                return first.get();
            }else {
                return null;
            }
        }
        return null;
    }

    @Override
    public void removeDeptInfo(Integer id) {
        DeptInfoTable deptInfoTable = baseMapper.selectById(id);
        deptInfoTable.setIsDeleted(1);
        baseMapper.updateById(deptInfoTable);
    }

    @Override
    public DeptTypeInfoVO selectDeptTypeInfo(Integer id) {
        DeptTypeInfoVO deptTypeInfoVO = new DeptTypeInfoVO();
        DeptTypeInfo deptTypeInfo = deptTypeInfoMapper.selectOne(Wrappers.<DeptTypeInfo>query().lambda().eq(DeptTypeInfo::getDeptId, id));
        if(deptTypeInfo==null){
            return null;
        }
        deptTypeInfoVO.setUntilTypeEnum(UntilTypeEnum.getEnum(deptTypeInfo.getUnitTypeCode()));
        deptTypeInfoVO.setPosition(deptTypeInfo.getPosition());
        return deptTypeInfoVO;
    }

    @Override
    public DeptMeasuringInfoVO selectDeptMeasuringInfo(Integer id) {
        DeptMeasuringInfoVO deptMeasuringInfoVO = new DeptMeasuringInfoVO();
        DeptMeasuringInfo deptMeasuringInfo = deptMeasuringInfoMapper.selectOne(Wrappers.<DeptMeasuringInfo>query().lambda()
                .eq(DeptMeasuringInfo::getDeptId, id));
        if(deptMeasuringInfo==null){
            return null;
        }
        deptMeasuringInfoVO.setMeasuringTypeCode(deptMeasuringInfo.getMeasuringTypeCode());
        deptMeasuringInfoVO.setMeasureTypeEnum(deptMeasuringInfo.getMeasuringTypeName());
        deptMeasuringInfoVO.setIntervalTime(deptMeasuringInfo.getIntervalTime());
        deptMeasuringInfoVO.setIsSelf(deptMeasuringInfo.getIsSelf());
        deptMeasuringInfoVO.setIsLightSource(deptMeasuringInfo.getIsLightSource());
        return deptMeasuringInfoVO;
    }

    @Override
    public DeptBenchInfoVO selectDeptBenchInfo(Integer id) {
        DeptBenchInfoVO deptBenchInfoVO = new DeptBenchInfoVO();
        DeptBenchInfo deptBenchInfo = deptBenchInfoMapper.selectOne(Wrappers.<DeptBenchInfo>query().lambda().eq(DeptBenchInfo::getDeptId, id));
        if(deptBenchInfo==null){
            return null;
        }
        deptBenchInfoVO.setBenchTypeNameEnum(deptBenchInfo.getBenchTypeName());
        deptBenchInfoVO.setPosition(deptBenchInfo.getPosition());
        deptBenchInfoVO.setPreviousDistance(deptBenchInfo.getPreviousDistance());
        deptBenchInfoVO.setParentBenchName(deptBenchInfo.getParentBenchName());
        deptBenchInfoVO.setBenchTypeCode(deptBenchInfo.getBenchTypeCode());
        deptBenchInfoVO.setXAxis(deptBenchInfo.getXAxis());
        deptBenchInfoVO.setYAxis(deptBenchInfo.getYAxis());
        return deptBenchInfoVO;
    }

    @Override
    public List<Integer> selectQuery(Integer id) {
        List<DeptInfoTable> deptInfoTables = baseMapper.selectList(Wrappers.<DeptInfoTable>query().lambda()
                .eq(DeptInfoTable::getParentId, id).eq(DeptInfoTable::getDeptCategory, DeptCategoryEnum.PLATFORM));
        if(!CollectionUtils.isEmpty(deptInfoTables)){
            return deptInfoTables.stream().map(DeptInfoTable::getId).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public MeasuringPageVO selectListBox(MeasuringPageDTO measuringPageDTO) {
        MeasuringPageVO measuringPageVO = new MeasuringPageVO();
        QueryWrapper<DeptInfoTable> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(DeptInfoTable::getDeptCategory,2).eq(DeptInfoTable::getIsDeleted,0);
        if(!StringUtils.isEmpty(measuringPageDTO.getMeasuringName())){
            queryWrapper.lambda().like(DeptInfoTable::getDeptName,measuringPageDTO.getMeasuringName());
        }
        Page<DeptInfoTable> deptInfoTablePage = baseMapper.selectPage(new Page<>(measuringPageDTO.getCurrent(), measuringPageDTO.getSize()), queryWrapper);
        measuringPageVO.setTotal(deptInfoTablePage.getTotal());
        measuringPageVO.setList(deptInfoTablePage.getRecords());
        return measuringPageVO;
    }

    @Override
    public DeptInfoTable selectMeasuring(Integer measuringId) {
        return baseMapper.selectById(measuringId);
    }

    @Override
    public  List<DeptBenchInfoSortVO> selectDeptBenchInfoByParent(Integer parentId) {

        List<DeptInfoTable> deptInfoTables = baseMapper.selectList(Wrappers.<DeptInfoTable>query().lambda().eq(DeptInfoTable::getParentId, parentId)
                .eq(DeptInfoTable::getIsDeleted, 0)
                .orderByAsc(DeptInfoTable::getSort));
        if(!CollectionUtils.isEmpty(deptInfoTables)){
            List<DeptBenchInfoSortVO> collect = deptInfoTables.stream().map(x -> {
                DeptBenchInfoSortVO deptBenchInfoSortVO = new DeptBenchInfoSortVO();
                deptBenchInfoSortVO.setSort(x.getSort());
                deptBenchInfoSortVO.setDeptName(x.getDeptName());
                deptBenchInfoSortVO.setId(x.getId());
                DeptBenchInfo deptBenchInfo = deptBenchInfoMapper.selectOne(Wrappers.<DeptBenchInfo>query().lambda().eq(DeptBenchInfo::getDeptId, x.getId()));
                if (deptBenchInfo == null) {
                    return null;
                }
                deptBenchInfoSortVO.setBenchTypeNameEnum(deptBenchInfo.getBenchTypeName());
                deptBenchInfoSortVO.setPosition(deptBenchInfo.getPosition());
                deptBenchInfoSortVO.setPreviousDistance(deptBenchInfo.getPreviousDistance());//
                deptBenchInfoSortVO.setParentBenchName(deptBenchInfo.getParentBenchName());
                deptBenchInfoSortVO.setBenchTypeCode(deptBenchInfo.getBenchTypeCode());
                deptBenchInfoSortVO.setXAxis(deptBenchInfo.getXAxis());//
                deptBenchInfoSortVO.setYAxis(deptBenchInfo.getYAxis());//
                deptBenchInfoSortVO.setParentBench(deptBenchInfo.getParentBench());
                return deptBenchInfoSortVO;

            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    @Override
    public List<DeptInfoTable> getMeasureTypeList(Integer id) {
        if(id==null){
            List<DeptInfoTable> deptInfoTables = baseMapper.selectList(Wrappers.<DeptInfoTable>query().lambda()
                    .eq(DeptInfoTable::getDeptCategory, DeptCategoryEnum.PLATFORM.getCode()).eq(DeptInfoTable::getIsDeleted,0));

            return deptInfoTables;
        }else {
            DeptInfoTable deptInfoTable = baseMapper.selectById(id);
            return Arrays.asList(deptInfoTable);
        }


    }

}
